//
//  ViewController.m
//  GCD常用函数
//
//  Created by 李煜 on 2024/3/20.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//  [self delay];
  [self barrier];
}
#pragma mark - 栅栏函数

- (void)barrier {
  //****栅栏函数不能使用全局并发队列
  dispatch_queue_t queue = dispatch_queue_create("download", DISPATCH_QUEUE_CONCURRENT);
  dispatch_async(queue, ^{
    for (int i = 0; i<100; i++) {
      NSLog(@"---download1--%@",[NSThread currentThread]);
    }
  });
  dispatch_async(queue, ^{
    for (int i = 0; i<100; i++) {
      NSLog(@"---download2--%@",[NSThread currentThread]);
    }
  });
  //栅栏函数 栅栏函数上面的任务都完成之后才会执行下面的download3
  dispatch_barrier_async(queue, ^{
    NSLog(@"++++++++++++++++++++++++++++++++++++++++++++");
  });
  dispatch_async(queue, ^{
    for (int i = 0; i<100; i++) {
      NSLog(@"---download3--%@",[NSThread currentThread]);
    }
  });

}

@end
